import axios from 'axios';
import { Notification } from 'element-ui';

const generatePostUrl = (url, type) => {
  let u = url.replace(/\./, '/');
  return type ? `/api/rest/${type}/${u}` : `/api/rest/${u}`;
};

export default {
  post (url, parameter, type = 'post') {
    return axios.post(generatePostUrl(url, type), parameter, {
      headers: {
        'Content-Type': 'text/plain;charset=UTF-8'
      }
    }).then(ret => {
      if (ret.data.code === '10020') {
        window.location.href = '/login/login.html';
      } else if (ret.data.code > 9999) {
        Notification.info({
          message: ret.data.message
        });
      } else if (ret.data.code !== 0 && ret.data.code !== '0') {
        Notification.error({
          message: ret.data.message
        });
      }
      return ret.data;
    }).catch(e => {
      Notification.error({
        message: 'error'
      });
    });
  }
};
